#include "SQLDropIndexStatement.h"
#include "SQLExprTableSource.h"
#include "SQLDDLStatement.h"
#include "../../visitor/SQLASTVisitor.h"

SQLDropIndexStatement::SQLDropIndexStatement()
{
}

SQLDropIndexStatement::SQLDropIndexStatement(DbType_ptr  dbType)
    : SQLStatementImpl(dbType)
{
}

SQLName_ptr SQLDropIndexStatement::getIndexName()
{
  return indexName;
}

void SQLDropIndexStatement::setIndexName(SQLName_ptr indexName)
{
  this->indexName = indexName;
}

SQLExprTableSource_ptr SQLDropIndexStatement::getTableName()
{
  return tableName;
}

void SQLDropIndexStatement::setTableName(SQLName_ptr tableName)
{
  this->setTableName(SQLExprTableSource_ptr(new SQLExprTableSource(std::dynamic_pointer_cast<SQLExpr>(tableName))));
}

void SQLDropIndexStatement::setTableName(SQLExprTableSource_ptr tableName)
{
  this->tableName = tableName;
}

SQLExpr_ptr SQLDropIndexStatement::getAlgorithm()
{
  return algorithm;
}

void SQLDropIndexStatement::setAlgorithm(SQLExpr_ptr x)
{
  if (x != NULL)
  {
    x->setParent(SharedObject(SQLDropIndexStatement));
  }
  this->algorithm = x;
}

SQLExpr_ptr SQLDropIndexStatement::getLockOption()
{
  return lockOption;
}

void SQLDropIndexStatement::setLockOption(SQLExpr_ptr x)
{
  if (x != NULL)
  {
    x->setParent(SharedObject(SQLDropIndexStatement));
  }
  this->lockOption = x;
}

void SQLDropIndexStatement::accept0(SQLASTVisitor_ptr visitor)
{
  if (visitor->visit(SharedObject(SQLDropIndexStatement)))
  {
    acceptChild(visitor, indexName);
    acceptChild(visitor, tableName);
    acceptChild(visitor, algorithm);
    acceptChild(visitor, lockOption);
  }
  visitor->endVisit(SharedObject(SQLDropIndexStatement));
}

SQLObject_list_ptr SQLDropIndexStatement::getChildren()
{
  SQLObject_list_ptr children = SQLObject_list_ptr();
  if (indexName != NULL)
  {
    children->push_back(std::dynamic_pointer_cast<SQLObject>(indexName));
  }
  if (tableName != NULL)
  {
    children->push_back(std::dynamic_pointer_cast<SQLObject>(tableName));
  }
  return children;
}

BOOL_ptr SQLDropIndexStatement::replace(SQLExpr_ptr expr, SQLExpr_ptr target)
{
  if (indexName == expr)
  {
    setIndexName(std::dynamic_pointer_cast<SQLName>(target));
    return BOOL::TRUE;
  }

  if (algorithm == expr)
  {
    setAlgorithm(target);
    return BOOL::TRUE;
  }

  if (lockOption == expr)
  {
    setLockOption(target);
    return BOOL::TRUE;
  }

  return BOOL::FALSE;
}

BOOL_ptr SQLDropIndexStatement::isIfExists()
{
  return ifExists;
}

void SQLDropIndexStatement::setIfExists(BOOL_ptr ifExists)
{
  this->ifExists = ifExists;
}

SQLDDLStatement::DDLObjectType SQLDropIndexStatement::getDDLObjectType()
{
  return SQLDDLStatement::DDLObjectType::INDEX;
}
